package com.yzcrm.utils;

import com.yzcrm.common.config.redis.Redis;
import com.yzcrm.common.config.redis.RedisManager;
import com.yzcrm.common.constant.BaseConstant;
import com.yzcrm.erp.crm.common.CrmEnum;

import java.util.Calendar;

/**
 * 公用工具类
 */
public class CommonUtil {

    /**
     * 生成编码
     *
     * @param label
     * @return
     */
    public synchronized static String generateCode(int label) {

        String code;

        Redis redis = RedisManager.getRedis();
        if (!redis.exists(BaseConstant.GENERATE_CODE_KEY)) {

            redis.set(BaseConstant.GENERATE_CODE_KEY, 0);
        }

        Integer num = redis.get(BaseConstant.GENERATE_CODE_KEY);

        Calendar calendar = Calendar.getInstance();
        code = "" + calendar.get(Calendar.YEAR) + (calendar.get(Calendar.MONTH) + 1) + calendar.get(Calendar.DAY_OF_MONTH) + (10000 + num);

        if (label == CrmEnum.PSI_PURCHASE_ENTER.getType()) {
            // 采购入库
            code = "CGRK" + code;
        } else if (label == CrmEnum.PSI_TEST_ENTER.getType()) {
            // 内部测试入库
            code = "CSRK" + code;
        } else if (label == CrmEnum.PSI_RETRUN_ENTER.getType()) {
            // 归还入库
            code = "GHRK" + code;
        } else if (label == CrmEnum.PSI_SALES_ENTER.getType()) {
            // 销售退货
            code = "THRK" + code;
        } else if (label == CrmEnum.PSI_INVENTORY_PROFIT_ENTER.getType()) {
            // 盘盈
            code = "PYRK" + code;
        } else if (label == CrmEnum.PSI_OTHER_ENTER.getType()) {
            // 其它入库
            code = "QTRK" + code;
        } else if (label == CrmEnum.PSI_REPAIR_ENTER.getType()) {
            // 维修入库
            code = "WXRK" + code;
        } else if (label == CrmEnum.PSI_SALES_LEAVE.getType()) {
            // 销售出库
            code = "XSCK" + code;
        } else if (label == CrmEnum.PSI_BORROW_LEAVE.getType()) {
            // 借用出库
            code = "JYCK" + code;
        } else if (label == CrmEnum.PSI_REPAIR_LEAVE.getType()) {
            // 维修出库
            code = "WXCK" + code;
        } else if (label == CrmEnum.PSI_TEST_LEAVE.getType()) {
            // 内部测试出库
            code = "CSCK" + code;
        } else if (label == CrmEnum.PSI_INVENTORY_LOSS_LEAVE.getType()) {
            // 盘亏
            code = "PKCK" + code;
        } else if (label == CrmEnum.PSI_OTHER_LEAVE.getType()) {
            // 其它出库
            code = "QTCK" + code;
        } else if (label == CrmEnum.FINANCE_OFFICE_PURCHASE.getType()) {
            // 办公用品
            code = "BGCG" + code;
        } else if (label == CrmEnum.FINANCE_NORMAL_PURCHASE.getType()) {
            // 常规备货
            code = "CGCG" + code;
        } else if (label == CrmEnum.FINANCE_PROJECT_PURCHASE.getType()) {
            // 项目备货
            code = "XMCG" + code;
        } else if (label == CrmEnum.FINANCE_DEV_PURCHASE.getType()) {
            // 研发用品
            code = "YFCG" + code;
        } else if (label == CrmEnum.FINANCE_OTHER_PURCHASE.getType()) {
            // 其它
            code = "QTCG" + code;
        } else if (label == CrmEnum.CRM_QUOTATION_COMBINATION.getType()) {
            // 报价
            code = "BJ" + code;
        } else if (label == CrmEnum.CRM_CONTRACT.getType()) {
            // 合同
            code = "HT" + code;
        }
        redis.set(BaseConstant.GENERATE_CODE_KEY, ++num);

        return code;
    }
}
